/*     */ package com.zimbra.cs.account.ldap;
/*     */ 
/*     */ import com.zimbra.common.account.Key.DomainBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.LogFactory;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.EntryCacheDataKey;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.extension.ExtensionUtil;
/*     */ import java.io.PrintStream;
/*     */ import java.util.Collections;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.concurrent.ConcurrentHashMap;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class DomainNameMappingHandler
/*     */ {
/*  42 */   private static Map<String, HandlerInfo> sHandlers = new ConcurrentHashMap();
/*  43 */   private static Log sLog = LogFactory.getLog(DomainNameMappingHandler.class);
/*     */   
/*     */ 
/*     */   public abstract String mapName(String paramString1, String paramString2, String paramString3)
/*     */     throws ServiceException;
/*     */   
/*     */ 
/*     */ 
/*     */   private static class HandlerInfo
/*     */   {
/*     */     Class<? extends DomainNameMappingHandler> mClass;
/*     */     
/*     */ 
/*     */ 
/*     */     public DomainNameMappingHandler getInstance()
/*     */     {
/*     */       DomainNameMappingHandler handler;
/*     */       
/*     */       try
/*     */       {
/*  63 */         handler = (DomainNameMappingHandler)this.mClass.newInstance();
/*     */       } catch (InstantiationException e) {
/*  65 */         handler = new DomainNameMappingHandler.UnknownDomainNameMappingHandler(null);
/*     */       } catch (IllegalAccessException e) {
/*  67 */         handler = new DomainNameMappingHandler.UnknownDomainNameMappingHandler(null);
/*     */       }
/*  69 */       return handler;
/*     */     }
/*     */   }
/*     */   
/*     */   private static HandlerInfo loadHandler(HandlerConfig handlerConfig) {
/*  74 */     HandlerInfo handlerInfo = new HandlerInfo(null);
/*  75 */     String className = handlerConfig.getClassName();
/*     */     try
/*     */     {
/*  78 */       handlerInfo.mClass = ExtensionUtil.findClass(className).asSubclass(DomainNameMappingHandler.class);
/*     */     }
/*     */     catch (ClassNotFoundException e) {
/*  81 */       sLog.warn("Domain name mapping handler %s for application %s not found", new Object[] { className, handlerConfig.getApplicaiton() });
/*     */       
/*     */ 
/*  84 */       handlerInfo.mClass = UnknownDomainNameMappingHandler.class;
/*     */     }
/*  86 */     return handlerInfo;
/*     */   }
/*     */   
/*     */   private static DomainNameMappingHandler getHandler(HandlerConfig handlerConfig) {
/*  90 */     String key = handlerConfig.getClassName();
/*  91 */     HandlerInfo handlerInfo = (HandlerInfo)sHandlers.get(key);
/*     */     
/*  93 */     if (handlerInfo == null) {
/*  94 */       handlerInfo = loadHandler(handlerConfig);
/*  95 */       sHandlers.put(key, handlerInfo);
/*     */     }
/*     */     
/*  98 */     return handlerInfo.getInstance();
/*     */   }
/*     */   
/*     */   private static class UnknownDomainNameMappingHandler extends DomainNameMappingHandler
/*     */   {
/*     */     public String mapName(String foreignName, String params, String zimbraDomainName) {
/* 104 */       return null;
/*     */     }
/*     */   }
/*     */   
/*     */   public static class HandlerConfig {
/*     */     String mApplication;
/*     */     String mClassName;
/*     */     String mParams;
/*     */     
/*     */     private HandlerConfig(String application, String className, String params) {
/* 114 */       this.mApplication = application;
/* 115 */       this.mClassName = className;
/* 116 */       this.mParams = params;
/*     */     }
/*     */     
/*     */     private String getApplicaiton() {
/* 120 */       return this.mApplication;
/*     */     }
/*     */     
/*     */     private String getClassName() {
/* 124 */       return this.mClassName;
/*     */     }
/*     */     
/*     */     private String getParams() {
/* 128 */       return this.mParams;
/*     */     }
/*     */   }
/*     */   
/*     */   public static HandlerConfig getHandlerConfig(Domain domain, String application) {
/* 133 */     Map<String, HandlerConfig> handlers = (Map)domain.getCachedData(EntryCacheDataKey.DOMAIN_FOREIGN_NAME_HANDLERS.getKeyName());
/*     */     
/*     */ 
/* 136 */     if (handlers == null) {
/* 137 */       handlers = new HashMap();
/*     */       
/* 139 */       String[] handlersRaw = domain.getForeignNameHandler();
/* 140 */       for (String handlerRaw : handlersRaw)
/*     */       {
/* 142 */         int idx1 = handlerRaw.indexOf(":");
/* 143 */         if (idx1 != -1) {
/* 144 */           String app = handlerRaw.substring(0, idx1);
/* 145 */           String className = handlerRaw.substring(idx1 + 1);
/* 146 */           String params = null;
/* 147 */           int idx2 = className.indexOf(":");
/* 148 */           if (idx2 != -1) {
/* 149 */             params = className.substring(idx2 + 1);
/* 150 */             className = className.substring(0, idx2);
/*     */           }
/* 152 */           handlers.put(app, new HandlerConfig(app, className, params, null));
/*     */         }
/*     */       }
/*     */       
/* 156 */       handlers = Collections.unmodifiableMap(handlers);
/* 157 */       domain.setCachedData(EntryCacheDataKey.DOMAIN_FOREIGN_NAME_HANDLERS.getKeyName(), handlers);
/*     */     }
/*     */     
/* 160 */     return (HandlerConfig)handlers.get(application);
/*     */   }
/*     */   
/*     */   public static String mapName(HandlerConfig handlerConfig, String foreignName, String zimbraDomainName) throws ServiceException {
/* 164 */     DomainNameMappingHandler handler = getHandler(handlerConfig);
/*     */     
/* 166 */     if ((handler instanceof UnknownDomainNameMappingHandler)) {
/* 167 */       throw ServiceException.FAILURE("unable to load domain name mapping handler " + handlerConfig.getClassName() + " for application:" + handlerConfig.getApplicaiton(), null);
/*     */     }
/*     */     
/* 170 */     return handler.mapName(foreignName, handlerConfig.getParams(), zimbraDomainName);
/*     */   }
/*     */   
/*     */   static class DummyHandler extends DomainNameMappingHandler
/*     */   {
/*     */     public String mapName(String foreignName, String params, String zimbraDomainName) throws ServiceException {
/* 176 */       return "user2@phoebe.mbp";
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 182 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/* 184 */     Domain domain = prov.get(Key.DomainBy.name, "phoebe.mbp");
/* 185 */     domain.addForeignName("app1:name1");
/* 186 */     domain.addForeignName("app2:name2");
/* 187 */     domain.addForeignName("app3:name3");
/*     */     
/* 189 */     domain.addForeignNameHandler("app1:com.zimbra.cs.account.ldap.DomainNameMappingHandler$DummyHandler:p1, p2, p3");
/*     */     
/*     */ 
/*     */ 
/* 193 */     Account acct = prov.getAccountByForeignName("user1@name1", "app1", null);
/* 194 */     acct = prov.getAccountByForeignName("user1@name1", "app1", null);
/* 195 */     System.out.println(acct.getName() + "(expecting user2@phoebe.mbp)");
/*     */     
/* 197 */     acct = prov.getAccountByForeignName("user1@name2", "app2", null);
/* 198 */     System.out.println(acct.getName() + "(expecting user1@phoebe.mbp)");
/*     */     
/* 200 */     acct = prov.getAccountByForeignName("user1", "app3", domain);
/* 201 */     System.out.println(acct.getName() + "(expecting user1@phoebe.mbp)");
/*     */     
/* 203 */     acct = prov.getAccountByForeignName("user1@name3", "app2", null);
/* 204 */     System.out.println(acct + "(expecting null)");
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/DomainNameMappingHandler.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */